package ttc.sftp.adapter;

import java.util.ArrayList;
import java.util.List;

import ttc.sftp.R;
import ttc.sftp.data.InstalledApp;
import ttc.sftp.data.VFile;
import ttc.sftp.data.VFile.Status;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyGirdAdapter extends BaseAdapter {

	LayoutInflater inflater;
	List<VFile> listFiles;

	public MyGirdAdapter(LayoutInflater inflater) {
		this.inflater = inflater;
		listFiles = new ArrayList<VFile>();
	}

	public void ClearAll() {
		listFiles.clear();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listFiles.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listFiles.get(position);
	}

	public void removeItem(int position) {
		listFiles.remove(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		if (row == null) {
			row = inflater.inflate(R.layout.list_item, parent, false);
		}
		ImageView icon = (ImageView) row.findViewById(R.id.list_item_icon);
		TextView name = (TextView) row.findViewById(R.id.list_item_name);
		TextView type = (TextView) row.findViewById(R.id.type);

		String filename = listFiles.get(position).getName();
		String extension = listFiles.get(position).getExt();
		if (extension.compareToIgnoreCase("") != 0)
			icon.setImageResource(InstalledApp.getIdIcon(extension));
		if (listFiles.get(position).IsDirectory())
			icon.setImageResource(R.drawable.icon_folder);
		name.setText(listFiles.get(position).getFileNameTrim());
		type.setText(listFiles.get(position).getStatus().toString());
		return row;
	}

	public void add(List<VFile> files, Status status) {
		// TODO Auto-generated method stub
		for (VFile file : files) {
			file.setStatus(status);
			listFiles.add(file);
		}
		this.notifyDataSetChanged();
	}

}
